﻿using GameCreator.Characters;
using UnityEngine;

namespace Gp.Scripts.Character {
    public class BodyLean : MonoBehaviour {
        public Transform RootBone;

        public bool RootBoneSpineLean = true;
        public bool RootBoneSpineMovement = true;

        public float RootBoneLeanIntensity = 30;
        public float RootBoneLeanSpeed = 8;
        public float RootBoneDownMovementIntensity = 0.5f;
        public float BlockForwardLeanWeight = 8;

        float Speed;
        float Lean;

        Vector3 NotAffectedEulerAngles;
        Vector3 NotAffectedUpward;

        public Axis AxisToLean;


        public GameCreator.Characters.Character character;


        public enum Axis {
            X,
            Y,
            Z
        }


        public void Start() {
            if (character == null) character = GetComponent<PlayerCharacter>();
            var anim = character.GetCharacterAnimator().animator;
            if (RootBone == null) RootBone = anim.GetBoneTransform(HumanBodyBones.Hips);
            anim.updateMode = AnimatorUpdateMode.AnimatePhysics;
        }


        void OnAnimatorIK() {
            NotAffectedEulerAngles = RootBone.localEulerAngles;
        }

        void LateUpdate() {
            DoInert();
        }

        void DoInert() {
            Vector3 euler = NotAffectedEulerAngles;
            NotAffectedUpward = RootBone.up;
            var characterVelocity = character.GetCharacterMotion();
            Speed = Mathf.Lerp(Speed, characterVelocity, 10 * Time.deltaTime);


            if (characterVelocity != 0) {
                Lean = Mathf.Lerp(Lean, (Speed * RootBoneLeanIntensity / BlockForwardLeanWeight),
                    RootBoneLeanSpeed * Time.deltaTime);
            }
            else {
                Lean = Mathf.Lerp(Lean, -(Speed * RootBoneLeanIntensity / 2), RootBoneLeanSpeed * Time.deltaTime);
                // if (JUFootPlacer != null && RootBoneSpineMovement) {
                //     JUFootPlacer.LastBodyPositionY -=
                //         RootBoneDownMovementIntensity * Mathf.Abs(Lean) / 10 * Time.deltaTime;
                // }
            }

            switch (AxisToLean) {
                case Axis.X:
                    euler.x += Lean;
                    break;
                case Axis.Y:
                    euler.y += Lean;
                    break;
                case Axis.Z:
                    euler.z += Lean;
                    break;
            }

            RootBone.localRotation = Quaternion.Euler(euler);
        }
// #if UNITY_EDITOR
//         private void OnDrawGizmos()
//         {
//             if (RootBone == null) return;
//             float angle = Vector3.SignedAngle(NotAffectedUpward, RootBone.up, RootBone.right);
//             if (angle == 0) return;
//
//             Color color = Color.Lerp(Color.green, Color.red, angle / 10);
//             Handles.color = color;
//             Handles.DrawWireArc(RootBone.position, -RootBone.right, RootBone.up, angle, 0.5f);
//
//             Color colortransparent = color; colortransparent.a = 0.1f;
//             Handles.color = colortransparent;
//             Handles.DrawSolidArc(RootBone.position, -RootBone.right, RootBone.up, angle, 0.5f);
//
//             Handles.DrawLine(RootBone.position, RootBone.position + RootBone.up * 0.5f);
//             Handles.color = Color.white;
//             Handles.DrawDottedLine(RootBone.position, RootBone.position + NotAffectedUpward * 0.5f, 2);
//             Handles.Label(RootBone.position + NotAffectedUpward * 0.6f, ((int)angle).ToString());
//         }
// #endif
    }
}